package Readers;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

import javax.swing.text.html.HTMLDocument;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import OddsObjects.WebsiteReaderTemplate;
import OddsObjects.Soccer.MoneyLine;
import OddsObjects.Soccer.OverUnder;

import com.sun.xml.internal.ws.api.pipe.NextAction;

public class SportsInteractionSoccerReader extends SoccerReaderTemplate {

	URL websiteUrl;
	
	
	SportsInteractionSoccerReader() {
		try {
			websiteUrl = new URL(urlConstants.URL_CONSTANT_SPORTS_INTERACTION);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public MoneyLine extractMoneyLineJSoup( URL pageUrl ) {
		
		Elements e = document.getElementsByClass("gameBettingContent");
		if(e.isEmpty()) {
			//this event does not exist properly...
			return null;
		}
		Elements moneyLineInfo = e.select("span.bettingType:matchesOwn(Moneyline):not(span.bettingType:contains( Moneyline)) + ul span");
		Elements moneyLineTeamNames = moneyLineInfo.select(".name");
		String teamA = moneyLineTeamNames.get(0).text();
		String teamB = moneyLineTeamNames.get(1).text();
		Elements moneyLineOdds = moneyLineInfo.select(".price");
		double oddsForTeamA = Double.valueOf(moneyLineOdds.get(0).text());
		double oddsForTeamB = Double.valueOf(moneyLineOdds.get(1).text());
		
		
		return new MoneyLine(oddsForTeamA, oddsForTeamB, teamA, teamB, new BettingWebsite());
	}
	
	@Override
	public MoneyLine extractMoneyLine(URL pageUrl) throws IOException {
		
		BufferedReader contentStream = getPageContent( pageUrl );
		
		String teamA = null, teamB = null;
		double oddsForTeamA, oddsForTeamB;
		Vector<Double> oddsVector = new Vector<Double>();
		String titleSeperator = " at ";
		
		Boolean insideMoneyLine = false; 
		//TODO: parse content into required fields
		String line = contentStream.readLine(), previousLine = "";
		while( line != null ){
			System.out.println(line);
		 	
		 	if(previousLine.contains("\"title\"")){
		 		String strippedLine = line.replaceAll("\\t", "");
		 		teamA = strippedLine.substring(line.lastIndexOf("\\t")+2, line.indexOf(titleSeperator));
		 		teamB = strippedLine.substring(line.indexOf(titleSeperator)+titleSeperator.length());
		 	}
		 	
		 	if(insideMoneyLine && line.contains("price")){
		 		//extract odds from between tags
		 		oddsVector.add(Double.valueOf(extractBetweenTags(line)));
		 	}
		 	
		 	if( insideMoneyLine  && line.contains("</ul>")) {
		 		break;
		 	}
		 	
		 	if (line.contains("<span class=\"bettingType\">Moneyline")) {
		 		insideMoneyLine = true;
		 	}

		 	previousLine = line;
			line = contentStream.readLine();
		}
		
		oddsForTeamA = oddsVector.elementAt(0);
		oddsForTeamB = oddsVector.elementAt(1);
		
		MoneyLine odds = new MoneyLine(oddsForTeamA, oddsForTeamB, teamA, teamB, new BettingWebsite());
		
		return odds;
	}

	private String extractBetweenTags(String line) {
		return( line.substring(line.indexOf(">")+1, line.indexOf("</")) );
	}

	@Override
	public OverUnder extractOverUnder() {
		// TODO Auto-generated method stub
		return null;
	}
	
	

}
